package listener;

import javax.servlet.ServletContext;
import javax.servlet.annotation.WebListener;
import javax.servlet.http.HttpSession;
import javax.servlet.http.HttpSessionEvent;
import javax.servlet.http.HttpSessionListener;

@WebListener
public class CountListener implements HttpSessionListener {
    private Integer count=0;

    @Override
    public void sessionCreated(HttpSessionEvent httpSessionEvent) {
        HttpSession session=httpSessionEvent.getSession();
        ServletContext application=session.getServletContext();

        synchronized (this) {
            application.setAttribute("count",++count);
        }

    }

    @Override
    public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
        HttpSession session=httpSessionEvent.getSession();
        ServletContext application=session.getServletContext();

        synchronized (this) {
            application.setAttribute("count",--count);
        }
    }
}
